package com.api.gbhg.luoao.la.action;

import com.api.gbhg.lbj.liubeijia.util.GBHGConstant2;
import com.api.gbhg.luoao.la.dao.BudgetModifyDao;
import com.api.gbhg.luoao.la.util.Allutils;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class BudgetClassPaymentAction  implements Action {

    public static BaseBean baseBean = new BaseBean();


    @Override
    public String execute(RequestInfo requestInfo) {

        try {
            //判断开关是否开启
            Map<String, Object> aSwitch = BudgetModifyDao.getSwitch();
            if (aSwitch.get("adjust").equals("0")) {//判断开关是否开启
                requestInfo.getRequestManager().setMessagecontent("预算调整未开启不能使用");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }

            int IscrossClass = 0;//是否垮类
            int IscrossPayment = 0;//是否跨款

            //获取调整项目 和 调整类型
            Map<String, Object> property = BudgetModifyDao.getProperty(requestInfo);

            if (property.get("dzlx").equals("0")) {

                //获取细化经济分类数据
                List<Map<String, Object>> list = BudgetModifyDao.getmxb(property.get("requestid").toString());

                //判断是否跨类跨款 以及 跨类是否可用

                for (int i = 0; i < list.size(); i++) {
                    Map<String, Object> map = list.get(i);

                    if (map.get("dzfx").toString().equals("1") || map.get("dzfx").toString().equals("3")) {

                        //获取对应的数据
                        List<Map<String, Object>> collect = list.stream().filter(x ->
                                x.get("bmyszcjjfl").equals(map.get("dzz").toString())).collect(Collectors.toList());
                        Map<String, Object> maps = collect.get(0);
                        List<String> zfjjfl = BudgetModifyDao.getzfjjfl(maps.get("bmyszcjjfl").toString());

                        String dyzfjjfls = map.get("dyzfjjfl").toString();//现在的
                        for (String s : zfjjfl) {

                            if (!s.equals(dyzfjjfls)) {//两个一样就不判断是否跨类跨款
                                //判断是否跨类 是否跨款
                                if (!s.substring(0, 3).equals(dyzfjjfls.substring(0, 3))) {//判断是否跨类

                                    IscrossClass = 1;

                                    if (aSwitch.get("adjustClass").equals("0")) {
                                        requestInfo.getRequestManager().setMessagecontent("开关未打开 [" + map.get("dyzfjjfl") + "] 不能跨款");
                                        requestInfo.getRequestManager().setMessageid("1");
                                        return FAILURE_AND_CONTINUE;
                                    }

                                } else if (!s.substring(0, 5).equals(dyzfjjfls.substring(0, 5))) {//判断是否跨款
                                    IscrossPayment = 1;
                                }

                            }
                        }

                    }

                }

                //判断金额是否 够扣
                RecordSetTrans recordSetTrans = new RecordSetTrans();
                Map<String, Object> adjustClass = BudgetModifyDao.getDepartmentBudgetadjustment(recordSetTrans
                        , property.get("requestid").toString(),
                        property.get("dzxm").toString(),"1");
                if (adjustClass.get("result").toString().equals("false")){
                    requestInfo.getRequestManager().setMessagecontent(""+adjustClass.get("mg"));
                    requestInfo.getRequestManager().setMessageid("1");
                    return FAILURE_AND_CONTINUE;
                }

            }

            RecordSetTrans recordSetTrans = new RecordSetTrans();

            //更新跨类 跨款
            boolean b = BudgetModifyDao.classPaymentUpdate(recordSetTrans, IscrossClass, IscrossPayment,
                    Allutils.getMaintableName(Allutils.getworkfolwid(property.get("requestid").toString())),
                    property.get("requestid").toString());

            if (b) return SUCCESS;



            requestInfo.getRequestManager().setMessagecontent("更新跨类款失败");
            requestInfo.getRequestManager().setMessageid("1");
            return FAILURE_AND_CONTINUE;

        }catch (Exception e){
            requestInfo.getRequestManager().setMessagecontent("代码错误："+e.toString());
            requestInfo.getRequestManager().setMessageid("1");
            return FAILURE_AND_CONTINUE;

        }

    }
}
